package cn.sdhuijin.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sdhuijin.dao.StandardDao;
import cn.sdhuijin.dao.StandardFolderDao;
import cn.sdhuijin.dao.StandardItemDao;
import cn.sdhuijin.dao.TargetDao;
import cn.sdhuijin.dao.UserDao;
import cn.sdhuijin.model.Standard;
import cn.sdhuijin.model.StandardFolder;
import cn.sdhuijin.model.StandardItem;
import cn.sdhuijin.model.Target;
import cn.sdhuijin.model.User;
import cn.sdhuijin.utils.CommonUtil;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年5月6日 下午5:04:09 类说明
 */

@Service
@Transactional
public class StandardServiceImpl implements StandardService {

	@Inject
	private StandardDao standardDao;
	@Inject
	private StandardFolderDao standardFolderDao;
	@Inject
	private StandardItemDao standardItemDao;
	@Inject
	private UserDao userDao;
	@Inject
	private TargetDao targetDao;

	@Override
	public void BatchImport(List<List<Object>> llo) throws Exception {
		if (llo != null) {
			Standard curStandard = new Standard();
			StandardFolder curStandardFolder = new StandardFolder();
			for (int i = 1; i < llo.size(); i++) {
				String standardName = llo.get(i).get(1).toString().trim();
				if (CommonUtil.allIsNotEmpty(standardName)) {
					List<Standard> existedStandard = standardDao
							.findByName(standardName);
					if (existedStandard.size() == 0) {
						Standard standard = new Standard();
						standard.setName(standardName);
						curStandard = standardDao.save(standard);
					} else {
						curStandard = existedStandard.get(0);
					}
				}
				String folderName = llo.get(i).get(2).toString().trim();
				if (CommonUtil.allIsNotEmpty(folderName)) {
					List<StandardFolder> existedStandardFolder = standardFolderDao
							.findByNameAndStandardId(folderName,
									curStandard.getId());
					if (existedStandardFolder.size() == 0) {
						StandardFolder standardFolder = new StandardFolder();
						standardFolder.setStandard(curStandard);
						standardFolder.setName(folderName);
						String folderScore = llo.get(i).get(3).toString()
								.trim();
						if (CommonUtil.allIsNotEmpty(folderScore)) {
							standardFolder.setScore(Double
									.parseDouble(folderScore));
						}
						curStandardFolder = standardFolderDao
								.save(standardFolder);
					} else {
						curStandardFolder = existedStandardFolder.get(0);
					}
				}
				StandardItem curStandardItem = new StandardItem();
				String description = llo.get(i).get(4).toString().trim();
				if (CommonUtil.allIsNotEmpty(description)) {
					List<StandardItem> existedStandardItem = standardItemDao
							.findByStandardFolderIdAndDescription(
									curStandardFolder.getId(), description);
					if (existedStandardItem.size() == 0) {
						curStandardItem.setDescription(description);
						String itemScore = llo.get(i).get(5).toString().trim();
						if (CommonUtil.allIsNotEmpty(itemScore)) {
							curStandardItem.setScore(Double
									.parseDouble(itemScore));
						}
						curStandardItem.setMethod(llo.get(i).get(6).toString()
								.trim());
						curStandardItem.setStandardFolder(curStandardFolder);
						standardItemDao.save(curStandardItem);
					}
				}
			}
		}
	}

	@Override
	public void ImportStandardTarget(List<List<Object>> llo) throws Exception {
		if (llo != null) {
			for (int i = 1; i < llo.size(); i++) {
				String targets = llo.get(i).get(1).toString().trim();
				String standardName = llo.get(i).get(2).toString().trim();
				if (CommonUtil.allIsNotEmpty(targets, standardName)) {
					String[] items = targets.split("/");
					Target parentTarget = null;
					boolean isExist = false;
					for (int j = 0; j < items.length; j++) {
						List<Target> existedTarget = null;
						if (j == 0) {
							existedTarget = targetDao.findByName(items[j]);
							if (existedTarget.size() > 0) {
								parentTarget = existedTarget.get(0);
								isExist = true;
							} else {
								isExist = false;
							}
						} else {
							if (isExist) {
								existedTarget = targetDao
										.findByNameAndParentId(items[j],
												parentTarget.getId());
								if (existedTarget.size() > 0) {
									parentTarget = existedTarget.get(0);
									isExist = true;
								} else {
									isExist = false;
								}
							}
						}
					}
					List<Standard> existedStandard = standardDao
							.findByName(standardName);
					if (existedStandard.size() > 0) {
						if (isExist) {
							parentTarget.setStandard(existedStandard.get(0));
						}
					}

				}
			}

		}
	}

	@Override
	public void ImportStandardDeductionUser(List<List<Object>> llo)
			throws Exception {
		if (llo != null) {
			for (int i = 1; i < llo.size(); i++) {
				String deductionUser = llo.get(i).get(1).toString().trim();
				String standardName = llo.get(i).get(2).toString().trim();

				if (CommonUtil.allIsNotEmpty(deductionUser, standardName)) {
					List<User> existedUser = userDao.findByName(deductionUser);
					List<Standard> existedStandard = standardDao
							.findByName(standardName);
					if (existedUser.size() > 0 && existedStandard.size() > 0) {
						existedStandard.get(0).addDeductionUser(
								existedUser.get(0));
						standardDao.save(existedStandard.get(0));
					}
				}
			}
		}
	}

}
